﻿using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using WxLib.Core.Cache;

namespace WxLib.Core
{
    /// <summary>
    /// 提供库的配置和常量。
    /// </summary>
    public static class Config
    {
        private static string _appData;

        /// <summary>
        ///  UserAgent用于HTTP请求
        /// </summary>
        public static readonly string UserAgent = "";// ConfigurationManager.AppSettings["wxLib.UserAgent"];

        static Config()
        {
            Separator = Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture);
            if (string.IsNullOrEmpty(UserAgent))
                UserAgent = "WxLib";
            _appData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WxLib");
            SetConfig();
        }

        /// <summary>
        /// 目录分隔符
        /// </summary>
        public static string Separator { get; set; }

        /// <summary>
        ///  获取或设置应用程序数据。
        /// </summary>
        /// <value>应用程序数据</value>
        public static string AppData
        {
            get { return _appData; }

            set
            {
                _appData = value;
            }
        }

        /// <summary>
        ///  获取或设置图像路径
        /// </summary>
        /// <value>图像路径</value>
        public static string ImagePath { get; set; }

        /// <summary>
        ///  获取或设置缓存路径
        /// </summary>
        /// <value>缓存路径</value>
        public static string CachePath { get; set; }

        /// <summary>
        ///  缓存工厂
        /// </summary>
        public static Func<string, IWxLibCache> CacheFactory { get; set; }

        private static void SetConfig()
        {
            CachePath = Path.Combine(AppData, "Cache");
            ImagePath = Path.Combine(AppData, "Images");
            CacheFactory = module => new WxLibFileCache(Path.Combine(CachePath, module), "register");
        }

    }
}